package com.gzrhhscd.service.impl;
import com.gzrhhscd.Pojo.Dept;
import com.gzrhhscd.Pojo.DeptLog;
import com.gzrhhscd.mapper.DeptMapper;
import com.gzrhhscd.mapper.EmpMapper;
import com.gzrhhscd.service.DeptLogService;
import com.gzrhhscd.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogService deptLogService;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

   @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws Exception {
       try {
           deptMapper.deleteByID(id);
//           int i=1/0;
//       if (true){throw new Exception("出错了了"); }
           empMapper.deleteEmpByDeptId(id);
       } finally {
           DeptLog deptLog=new DeptLog();
           deptLog.setCreateTime(LocalDateTime.now());
           deptLog.setDescription("执行了解散部门的操作，此次解散的是"+id+"号部门");
           deptLogService.insert(deptLog);
       }
   }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }
}
